//
//  TopViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "TopViewController.h"
#import "TopImageCell.h"
#import "TopDetailViewController.h"
@interface TopViewController ()
{
    NSMutableArray *dataArr;
    
}
@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self configUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)loadData {
    
    dataArr = [NSMutableArray array];
    
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"top250" ofType:@"json"] ] options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *arr = dataDic[@"subjects"];
    
    for (NSDictionary *dic in arr) {
        
        TopImagemodel *model = [[TopImagemodel alloc] init];
        
        model.rating = dic[@"rating"];
        model.image = dic[@"images"][@"large"];
        model.title = dic[@"title"];
//        NSLog(@"%@",model.title);
        
        [dataArr addObject:model];
    }
    

    
}

- (void)configUI {
    
    self.collectionFlowLayout.itemSize = CGSizeMake((kScreenWidth - 60) / 3 , kScreenHeight / 4);
    self.collectionFlowLayout.minimumLineSpacing = 10;
    self.collectionFlowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 0, 15);
    
    
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TopImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TopImageCell" forIndexPath:indexPath];
    
    cell.model = dataArr[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return dataArr.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopDetailViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"TopDetailViewController"];
    [self.navigationController pushViewController:viewCtrl animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
